'use strict';

const { Controller } = require('egg');

class SwiperController extends Controller {

    async list() {
        const { ctx, app } = this;
        const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 10;
        const pageNumber = ctx.query.pageNumber ? parseInt(ctx.query.pageNumber) : 1;
        let where = {};
        let { count, rows } = await app.model.Swiper.findAndCountAll({
            where: where,
            offset: (pageNumber - 1) * pageSize,
            limit: pageSize,
        });
        // 返回用户信息和token
        ctx.apiSuccess({
            totalCount: count,
            currPage: pageNumber,
            list: rows
        });
    };


    async addSwiper() {
        const { ctx, app } = this;
        ctx.validate({
            image: {
                type: 'url',
                required: true,
                desc: "图片路径"
            },
            link: {
                type: 'string',
                required: true,
                desc: '跳转链接',
                default: ''
            },
            order: {
                type: 'int',
                required: true,
                desc: '排序值'
            }
        });
        const { image, link, order } = ctx.request.body;
        await app.model.Swiper.create({
            image,
            link,
            order
        });
        ctx.apiSuccess('添加成功')
    };

    async updateSwiper() {
        const { ctx, app } = this;
        ctx.validate({
            id: {
                type: 'int',
                required: true,
                desc: '轮播图id',
            },
            image: {
                type: 'url',
                required: true,
                desc: "图片路径"
            },
            link: {
                type: 'string',
                required: true,
                desc: '跳转链接',
                default: ''
            },
            order: {
                type: 'int',
                required: true,
                desc: '排序值'
            }
        });
        const { image, link, order, id } = ctx.request.body;
        let swiper = await app.model.Swiper.findOne({
            where: {
                id: id
            }
        });
        if (!swiper) {
            ctx.throw('更新失败,不存在此轮播图')
        };
        swiper.image = image;
        swiper.link = link;
        swiper.order = order;
        await swiper.save();
        ctx.apiSuccess('更新成功');
    }

    async deleteSwiper() {
        const { ctx, app } = this;
        ctx.validate({
            ids: {
                type: 'all',
                desc: '轮播图ids'
            }
        });
        let { ids } = ctx.request.body;
        // console.log(JSON.parse(ids))
        ids = JSON.parse(ids);
        await app.model.Swiper.destroy({
            where: {
                id: ids
            }
        });
        ctx.apiSuccess('删除成功');
    }

    async detail() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        let swiper = await app.model.Swiper.findOne({
            where: {
                id: id
            }
        });
        ctx.apiSuccess(swiper);
    }
}

module.exports = SwiperController;